# 图的抽象数据类型
class Graph:
    def __init__(self,n,m,dir=True,w=True):
        self.verNum= n
        self.edgeNum=m
        self.vertex=[0]*n
        self.dir,self.wt=dir,w
        self.edge=[[0 if self.wt else float('inf')
                    for i in range(self.verNum)] for  j in range(self.verNum)]
    def addVerterx(self,ls):
        self.vertex=ls
    def addEdge(self,fr,to,w=1):
        ifr=self.vertex.index(fr)
        ito=self.vertex.index(to)
        if self.dir:
            self.edge[ifr][ito]=self.edge[ito][ifr]=w
        else:
            self.edge[ifr][ito]=w
    def printformat(self):
        print('      ',end='')
        for i in self.vertex:
            print('%4s'%i,end='  ')
        print()
        for i in range(self.verNum):
            print('%4s'%self.vertex[i],end='  ')
            for j in range(len(self.edge[0])):
                print('%4s'%self.edge[i][j],end='  ')
            print()
    # 打印度数
    def printDegree(self):
        result=[]
        sum=0
        for i in range(self.verNum):
            for j in range(len(self.edge[0])):
                sum+=self.edge[i][j]
            result.append(sum)
            sum=0
        for i in range(self.verNum):
            print('节点:%s 度数%s'%(self.vertex[i],result[i]))


    def print(self):
        print("顶点数：%d,边数：%d"%(self.verNum,self.edgeNum))
        print("顶点:",self.vertex)
        print("边",self.edge)

# 读入数据
n, m = map(int, input().split())
g = Graph(n, m)
g.addVerterx(list(input().split()))  # 输入顶点
for i in range(m):  # 输入m条边
    fr, to, w = input().split()
    g.addEdge(fr, to, float(w))
g.print()
g.printformat()
g.printDegree()


